﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Item : MonoBehaviour
{
    
    public enum ItemType { 
        Head = -1, 
        LightBall = 0, 
        HeavyBall = 1 
    };

    public static Dictionary<ItemType, Head.State> ItemStateDict = new Dictionary<ItemType, Head.State>
    {
        { ItemType.Head, Head.State.Normal },
        { ItemType.LightBall, Head.State.Light },
        { ItemType.HeavyBall, Head.State.Heavy }
    };
    public ItemType type;

    /* item skill logic */
    public virtual bool HasSkill // for now all items have skills (that is, can work)
    {
        get
        {
            return true;
        }
    }
    public virtual bool CanWork
    {
        get
        {
            return HasSkill && (power > workingCost);
        }      
    }
    public bool working; // true: this item is being used
    public Head workingTarget; // head that the item is working on
    public float workingCost = 50;

    /* follow logic */

    public bool follow;
    public GameObject follow_target;
    public virtual bool Following // true: this item is collected
    {
        get
        {
            return follow && (follow_target != null) && (type != ItemType.Head);
        } 
    }

    /* power logic */
    public static RectTransform FindBar(ItemType type)
    {
        if (type == ItemType.LightBall)
            return GameObject.FindGameObjectWithTag("LightBar").GetComponent<RectTransform>();
        else if (type == ItemType.HeavyBall)
            return GameObject.FindGameObjectWithTag("HeavyBar").GetComponent<RectTransform>();
        else
            return null;
    }

    float fullPowerHeight;
    public virtual float power
    {
        get
        {
            return FindBar(type).rect.height / fullPowerHeight * powerMax;
        }
        set
        {
            Debug.Log(value);
            float height = value / powerMax * fullPowerHeight;
            var transf = FindBar(type);
            transf.sizeDelta = new Vector2(transf.rect.width, height);
        }
    }
    public float powerMax = 100;
    public float powerConsumeRate = 10f;
    public float powerRecoveryRate = 15f;
    public virtual bool Recovering
    {
        get
        {
            return !working && Following;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        // GetComponent<SpriteRenderer>().sprite = 
        /* item skill logic */
        // This part has been moved to HeadSkill
        // if (type == ItemType.Head)
        //    workingTarget = GetComponent<Head>();

        /* follow logic */
        follow = false;
        working = false;
        fullPowerHeight = FindBar(type).rect.height;
        /* power logic */
        power = powerMax;
    }

    private void Update()
    {
        UpdatePower();
        // Example of manipulating power
        // GetComponentInChildren<Text>().text = ((int)power).ToString() + "/" + ((int)powerMax).ToString();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Following)
        {
            return;

            var body = GetComponent<Rigidbody2D>();
            var vec = follow_target.transform.position - body.transform.position;
            var minvec = vec.normalized * 1.5f;

            if (vec.magnitude > minvec.magnitude)
            {
                Vector2 new_velocity = (vec - 1.1f * minvec) / Time.deltaTime * 0.2f;
                body.velocity = 0.5f * body.velocity + 0.5f * new_velocity;
            }
            else
                body.velocity = body.velocity * 0.8f;// new Vector2(); follow_target.GetComponent<Rigidbody2D>().velocity * 0.2f + 

            //body.velocity /= 2;
        }
    }

    public void SetFollowTarget(Head head, GameObject target)
    {
        workingTarget = head;
        follow_target = target;
        follow = true;
        GetComponent<SpriteRenderer>().enabled = false;
        GetComponent<Collider2D>().enabled = false;
    }

    public virtual void StartWorking()
    {
        working = true;
        power -= workingCost;
    }

    public virtual void EndWorking()
    {
        working = false;
    }

    public virtual void UpdatePower()
    {
        if (Recovering)
        {
            power = Mathf.Min(power + Time.deltaTime * powerRecoveryRate, powerMax);
        } else if (working)
        {
            Debug.Log("I'm working");
            float t_power = power - Time.deltaTime * powerConsumeRate;
            if (t_power < 0)
            {
                t_power = 0;
                power = t_power;
                workingTarget.EndWorkingItem();
            } else
            {
                power = t_power;
            }
        }
    }
}
